import request from '@/utils/request'

const base_uri = '/demo'
/**
 * 获取详情
 * @param id
 * @returns {*|void|AxiosPromise<any>}
 */
function detailData(id) {
  return request.post(base_uri + '/get', {
    id: id
  })
}

/**
 * 保存数据
 */
function save(editFormData) {
  // 格式化上传数据
  const saveParam = Object.assign({}, editFormData)
  saveParam.birthday = new Date(saveParam.birthday).format('yyyy-MM-dd')
  // 提交保存
  return request.post(base_uri + '/save', saveParam)
}

/**
 * 分页查询
 * @param searchForm
 * @param pagenum
 * @param pagesize
 * @returns {Promise<T>}
 */
function queryByPage(searchForm = {}, pagenum = 0, pagesize = 10) {
  // 添加查询参数
  const searchParam = Object.assign({}, searchForm)
  // 添加分页号
  searchParam.pagenum = pagenum
  searchParam.pagesize = pagesize

  // 发送到后台的页号最小为0
  if (isNaN(searchParam.pagenum) || Number(searchParam.pagenum) < 1) {
    searchParam.pagenum = 0
  } else {
    searchParam.pagenum -= 1
  }

  // 发送ajax数据请求
  return request.post(base_uri + '/query', searchParam).then(result => {
    const localPageData = {}
    // 查询完成数据格式化完成之后，统一赋值
    localPageData.datalist = result.datalist
    // 查询结果中的页号+1
    localPageData.pagenum = Number(result.pagenum) + 1
    localPageData.totalelements = Number(result.totalelements)
    localPageData.pagesize = Number(result.pagesize)
    // 继续执行调用链
    return Promise.resolve(localPageData)
  })
}

/**
 * deleteData
 */
function deleteData(id) {
  // 提交保存
  return request.post(base_uri + '/delete', {
    id: id
  })
}

// 导出结果数据
export default {
  detailData,
  save,
  queryByPage,
  deleteData
}
